<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\BaseController;
use App\Services\Tools\CommonService;
use Carbon\Carbon;
use Illuminate\Http\Request;    //引用http请求类
use App\Exceptions\BasicException;  //引入框架异常处理类
use Illuminate\Support\Facades\Hash;    //引入hash
use App\Services\Cache\AdminCacheService;   //admin缓存
use App\Services\Tools\JwtService;    //引入JWT服务
use App\Services\Tools\FileService;   //引入文件处理类
use App\Response\Response;
use App\Models\MainDB\GiveGiftDay;

class GiftTypeController extends BaseController {

    /**
     * @api {post} /gift_type/list 礼物分类列表
     * @apiVersion 0.1.0
     * @apiName 礼物分类列表
     * @apiGroup 礼物管理
     * @apiParam {string} name    礼物名字，非必传
     * @apiSuccessExample 成功响应
    {
        "code": 200,
        "data": [
                {
                    "id（主键id）": 1,
                    "name（名称）": "房间礼物",
                    "add_time（添加时间）": "2020-08-01 12:09;51"
                }
            ]
    }
     *
     */
    public function giftTypeList(Request $request) {
        $name = $request->input('name', '');
        $where =[];
        if ($name){
            $where[] =['name', 'like', "%{$name}%"];
        }
        $giftTypeList= app('repo_gift_type')->giftTypeList($where);
        if ($giftTypeList){
            foreach ($giftTypeList as  $key=>&$value){
                $value['add_time'] =date('Y-m-d H:i:s',$value['add_time']);
            }
        }
        return Response::sendData($giftTypeList);
    }


    /**
     * @api {post} /gift_type/add 添加礼物分类
     * @apiVersion 0.1.0
     * @apiName 添加礼物分类
     * @apiGroup 礼物管理
     *
     * @apiParam {string} name   礼物名称，必传

     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93025  添加礼物分类失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93025,
     *          "message": "errorMsg"
     *      }
     *
     */

    public  function  addGiftType(Request $request){
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'name' => 'required|max:255',
            ]
        );
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));
        $param = $request->only(['name']);
        $param['add_time'] =time();
        app('repo_gift_type')->addGiftType($param);
        return Response::sendData(true);
    }

    /**
     * @api {post} /gift_type/save 编辑礼物分类
     * @apiVersion 0.1.0
     * @apiName 编辑礼物分类
     * @apiGroup 礼物管理
     *
     * @apiParam {number} id   主键ID，必传
     * @apiParam {string} name   礼物名称，必传

     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93026  编辑礼物分类失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93026,
     *          "message": "errorMsg"
     *      }
     *
     */
    public  function  saveGiftType(Request $request){
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'id' => 'required|numeric',
                'name' => 'required|max:255',
            ]
        );
        if ($v->fails()) throw new BasicException(10001, getValidateMessage($v->messages()));
        $param = $request->only(['id','name']);
        app('repo_gift_type')->saveGiftType($param);
        return Response::sendData(true);

    }

    /**
     * @api {post} /gift_type/del 删除礼物分类
     * @apiVersion 0.1.0
     * @apiName 删除礼物分类
     * @apiGroup 礼物管理
     *
     * @apiParam {string} ids   删除的礼物主键（多个用逗号隔开），必传

     * @apiSuccessExample 成功响应
     * {"code":200,"data":true}
     * @apiError 93027  添加礼物失败
     * @apiErrorExample 错误响应
     *
     *      {
     *          "code": 93027,
     *          "message": "errorMsg"
     *      }
     *
     */

    public  function  delGiftType(Request $request){
        //参数校验
        $v = app('validator')->make(
            $request->all(),
            [
                'ids' => 'required',
            ]
        );
        if ($v->fails()) throw new BasicException(10001);
        $ids= $request->input('ids');
        $arr_ids = explode(',',$ids);
        app('repo_gift_type')->delGiftType($arr_ids);
        return Response::sendData(true);
    }

}
